package cn.mumzone.pos.ui.fragment.member;

import android.content.Intent;
import android.os.Bundle;
import android.text.InputFilter;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.TextView;

import com.ruffian.library.widget.RLinearLayout;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;
import cn.mumzone.basic.base.BaseFragment;
import cn.mumzone.basic.util.EditTextUtil;
import cn.mumzone.basic.util.NetUtil;
import cn.mumzone.basic.util.PosToast;
import cn.mumzone.basic.util.RxBus;
import cn.mumzone.pos.AppConstants;
import cn.mumzone.pos.R;
import cn.mumzone.pos.api.ApiRequest;
import cn.mumzone.pos.event.RefreshMemberLevelEvent;
import cn.mumzone.pos.model.MemberEntity;
import cn.mumzone.pos.presenter.MemberPresenter;
import cn.mumzone.pos.presenter.contract.MemberContract;
import cn.mumzone.pos.ui.activity.ACT_AddOrModifyMember;
import cn.mumzone.pos.ui.activity.ACT_MemberDetail;
import cn.mumzone.pos.ui.widgets.keyboard.PosKeyBoardView;
import cn.mumzone.pos.util.PopupUtil;

/**
 * Created By Dlx
 * on 2019/7/3
 */
public class FRA_Member extends BaseFragment<MemberPresenter> implements MemberContract.View {

    @BindView(R.id.tv_add_member)
    TextView tvAddMember;
    @BindView(R.id.et_search_member)
    EditText etSearchMember;
    @BindView(R.id.bill_delete_iv)
    ImageView deleteIv;
    @BindView(R.id.btn_search)
    RadioButton memberSearchRb;
    @BindView(R.id.ll_input_member)
    RLinearLayout llInputMember;
    @BindView(R.id.kb_search_member)
    PosKeyBoardView kbSearchMember;

    private PopupWindow mPopUp;

    @Override
    protected void onUserVisible() {
        RxBus.getDefault().post(new RefreshMemberLevelEvent());
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        if (hidden) {
            if (mPopUp != null) {
                mPopUp.dismiss();
                mPopUp = null;
            }
        }
    }

    @Override
    protected void initEventAndData() {
        etSearchMember.setFilters(new InputFilter[]{new InputFilter.LengthFilter(11)});

        EditTextUtil.hideSoftInput(mContext, etSearchMember);

        kbSearchMember.setOnConfirmClickListener(new PosKeyBoardView.OnConfirmClickListener() {
            @Override
            public void onConfirmClick(String text) {
                mPresenter.searchMember(etSearchMember.getText().toString().trim());
            }
        });

        showPopupHint();

        isPopupHintPrepared = true;

    }

    @Override
    protected int getLayout() {
        return R.layout.fra_member;
    }

    @OnClick({R.id.tv_add_member})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_add_member:

                PopupUtil.dismissPopup(PopupUtil.MEMBER_ADD_NEW, mPopUp);

                if (!NetUtil.isNetworkAvailable(mContext)) {
                    PosToast.INSTANCE.makeShortToast(mContext, "网络不给力，请稍候再试");
                    return;
                }

                startActivity(new Intent(getActivity(), ACT_AddOrModifyMember.class));
                break;
        }
    }

    @Override
    public void showSearchMemberSuccess(MemberEntity memberEntity) {
        Bundle data = new Bundle();
        data.putString("memberPhone", etSearchMember.getText().toString().trim());
        data.putString("memberId", memberEntity.getMemberId());
        readyGo(ACT_MemberDetail.class, data);
    }

    @Override
    public void showToast(String str) {

    }

    @Override
    public void noSuchMember(String string) {
        PosToast.INSTANCE.makeShortToast(mContext, string);
    }

    @Override
    public void showNetError() {
        PosToast.INSTANCE.makeShortToast(mContext, "网络不给力，请稍候再试");
    }

    @Override
    public void onStop() {
        super.onStop();
        etSearchMember.setText("");
    }

    @Override
    protected boolean hasViewPermission() {
        return AppConstants.TEMP_USER_PERMISSION != null && AppConstants.TEMP_USER_PERMISSION.isMember();
    }

    @Override
    public void showPopupHint() {
        tvAddMember.post(new Runnable() {
            @Override
            public void run() {
                mPopUp = PopupUtil.showBlackBgPopup(tvAddMember, PopupUtil.MEMBER_ADD_NEW, "点击添加会员", PopupUtil.LEFT);
            }
        });
    }

    @Override
    public boolean hasPopupHint() {
        return true;
    }

    @Override
    public void hidePopupHint() {
        if (mPopUp != null) {
            mPopUp.dismiss();
            mPopUp = null;
        }
    }

}
